﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using QLCHBANGIAY.BUSINESS;
using QLCHBANGIAY.DATA;
using System.Collections;

namespace QuanLyBanGiay
{
    public partial class timkiemnangcao : System.Web.UI.Page
    {
        SortedList ds;
        void Load_PhanLoaiSP()
        {
           // List<BizLoaiSP> loaiSPs = BizLoaiSP.GetAll();
            List<Category> loaiSPs = CategoryServices.Category_GetByTop("100", "", " Id desc");
            ddlPhanLoai_Search.DataSource = loaiSPs;
            ddlPhanLoai_Search.DataTextField = "Tag";
            ddlPhanLoai_Search.DataValueField = "id";
            ddlPhanLoai_Search.DataBind();
            ddlPhanLoai_Search.Items.Insert(0, "Loại Sản phẩm");
        }
        protected void Page_Load(object sender, EventArgs e)
        {

            string[] text = { "Giá", "100.000 VNĐ", "300.000 VNĐ", "500.000 VNĐ", "700.000 VNĐ", "1.000.000 VNĐ", "1.500.000 VNĐ", "2.000.000 VNĐ", "5.000.000 VNĐ", "7.000.000 VNĐ" };
            double[] value = { 0, 100000, 300000, 500000, 700000, 1000000, 1500000, 2000000, 5000000, 7000000 };
            ds = new SortedList();
            for (int i = 0; i < value.Count(); i++)
            {
                ds.Add(value[i], text[i]);
            }

            if (!IsPostBack)
            {
                Load_PhanLoaiSP();
                ddlGiaDen.DataSource = ds;
                ddlGiaDen.DataTextField = "value";
                ddlGiaDen.DataValueField = "key";
                ddlGiaDen.DataBind();
            }

           // List<Category> listca = new List<Category>();
           //// listca = CategoryServices.Category_GetByAll("");
           // listca = CategoryServices.Category_GetByTop("100", "", " Id desc");
           //   //  LiteralLoai.Text += "<option value='" + rd["maloai"] + "'>" + rd["tenloai"] + "</option>";
           // if (listca.Count > 0)
           // {
           //     for (int i = 0; i < listca.Count; i++)
           //     {

           //      //   Chuoi += "<li><a href=\"Phanloaisanpham.aspx?tag=" + list[i].Tag + "\">" + list[i].Name + "</a></li>";
           //         LiteralLoai.Text += "<option value='" + listca[i].Tag + "'>" + listca[i].Name + "</option>";
           //     }
           // }
           
            
        }

        //protected void Button1_Click(object sender, EventArgs e)
        //{

                 
       
        ////        sanpham[] dssanpham = new sanpham[200];

        ////        entity et = new entity();
        ////        string sql = "";
        ////        if (Page.Request.QueryString["Maloai"] == null) // neu khong co loi thi hien thi binh thuong
        ////        {
        ////            sql = "select MaSP,MaLoai,TenSP,Gia,Hinh from sp where sudung=1";
        ////        }
        ////        else
        ////        {
        ////            sql = "select MaSP,MaLoai,TenSP,Gia,Hinh from sp where MaLoai=" + Page.Request.QueryString["Maloai"] + "and sudung=1";
        ////        }
        ////        if (Tuk != null)
        ////        {
        ////            sql = "select MaSP,MaLoai,TenSP,Gia,Hinh from sp where tensp like '%" + Tuk + "%'" + "and sudung=1";
        ////        }
        ////        string tensp = Page.Request.QueryString["tensp"];
        ////        string loai = Page.Request.QueryString["loai"];
        ////        string giatu = Page.Request.QueryString["giatu"];
        ////        string giaden = Page.Request.QueryString["giaden"];
        ////        if (tensp != null || loai != null || giatu != null || giaden != null)
        ////        {
        ////            sql = "select MaSP,MaLoai,TenSP,Gia,Hinh from sp where sudung=1 ";
        ////            if (giatu != "" || giaden != "")
        ////                sql += "and gia Between " + giatu + " and  " + giaden;
        ////            if (loai != "")
        ////                sql += " and maloai='" + loai + "' ";
        ////            if (tensp != "")
        ////                sql += " and tensp like '%" + tensp + "%'";


        ////        }

        ////        SqlDataReader rd = et.LayThongTin(sql);
        ////        int dem = 0;


        ////    }
        ////    catch (Exception ex)
        ////    {

        ////    }
        //}
      
        void ShowMessage(string mTextMsg, string mControlFocus)
        {
            System.Text.StringBuilder sb = new System.Text.StringBuilder("");
            sb.Append("<script language=\"JavaScript\">");
            if (mTextMsg != "") sb.Append("  alert('" + mTextMsg + "');");
            if (mControlFocus != "") sb.Append("document.forms[0].item('" + mControlFocus + "').focus();");
            sb.Append("</script>");
            if (!Page.IsStartupScriptRegistered("setFocus")) Page.RegisterStartupScript("setFocus", sb.ToString());
        }

        protected void btnTim_kiem_Click(object sender, EventArgs e)
        {
            string ten = string.Empty;
            int idLoaisp = 0;
            double giaTu = 0;
            double giaDen = 0;

            if (txtTim_kiem.Text == "Từ khóa tìm kiếm" && ddlPhanLoai_Search.Text == "Loại Sản phẩm"
                && ddlGiaTu.SelectedIndex == 0 && ddlGiaDen.SelectedIndex == 0)
            {
                ShowMessage("Bạn chưa chọn thông tin tìm kiếm", "btnTim_kiem");
                return;
            }
            if (txtTim_kiem.Text != null)
            {
                ten = txtTim_kiem.Text.Trim();
            }
            if (txtTim_kiem.Text == "Từ khóa tìm kiếm")
            {
                ten = string.Empty;
            }
            if (ddlPhanLoai_Search.SelectedIndex > 0)
                idLoaisp = int.Parse(ddlPhanLoai_Search.SelectedValue);
            if (ddlGiaTu.SelectedIndex > 0)
                giaTu = double.Parse(ddlGiaTu.SelectedValue);
            if (ddlGiaDen.SelectedIndex > 0)
                giaDen = double.Parse(ddlGiaDen.SelectedValue);
            //if (giaTu > giaDen)
            //{
            //    double tam = giaTu;
            //    giaTu = giaDen;
            //    giaDen = tam;
            //}
            Response.Redirect(string.Format("Default.aspx?ten={0}&ploai={1}&giaTu={2}&giaDen={3}", ten, idLoaisp, giaTu, giaDen));
  

        }

        protected void btnHuy_bo_Click(object sender, EventArgs e)
        {
            txtTim_kiem.Text = "Từ khóa tìm kiếm";
            ddlPhanLoai_Search.SelectedIndex = 0;
            ddlGiaTu.SelectedIndex = 0;
            ddlGiaDen.SelectedIndex = 0;
        }

        protected void ddlGiaTu_SelectedIndexChanged(object sender, EventArgs e)
        {
            double giatu = Convert.ToDouble(ddlGiaTu.SelectedValue);
            for (int i = ds.Count - 1; i > 0; i--)
            {
                double giaden = Convert.ToDouble(ds.GetKey(i));
                if (giaden <= giatu)
                {
                    ds.RemoveAt(i);
                }
            }
            ddlGiaDen.DataSource = null;
            ddlGiaDen.DataSource = ds;
            ddlGiaDen.DataTextField = "value";
            ddlGiaDen.DataValueField = "key";
            ddlGiaDen.DataBind();
        }

    }
}